package com.yunyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunyi.common.annotation.Log;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.common.core.domain.TreeSelect;
import com.yunyi.common.core.domain.entity.SysAreaA;
import com.yunyi.common.core.domain.entity.SysOffice;
import com.yunyi.common.enums.BusinessType;
import com.yunyi.common.utils.StringUtils;
import com.yunyi.system.service.SysAreaAService;
import com.yunyi.system.service.SysOfficeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: SysOfficeController
 * @Author: ZouLi
 * @Description: 机构控制器
 * @Date: 2023/8/29 16:32
 **/
@RestController
@RequestMapping("/system/office")
@Api(tags = "机构控制器")
public class SysOfficeController {

    /**
     * OfficeService
     **/
    @Autowired
    private SysOfficeService officeService;

    /**
     * SysAreaService
     **/
    @Autowired
    private SysAreaAService areaService;

    /**
     * @Description: 查询前端机构树状图数据
     * @Param [office]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/officeTree")
    public AjaxResult getOfficeTree(SysOffice office) {
        if (StringUtils.isNull(office)) {
            office = new SysOffice();
        }
        return AjaxResult.success(officeService.selectOfficeTreeList(office));
    }

    /**
     * @Description: 查询前端机构树状列表数据
     * @Param [office]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/officeTreeList")
    @ApiOperation(value = "获取树状列表")
    public AjaxResult getOfficeTreeList(SysOffice office) {
        // 查询机构数据
        List<SysOffice> offices = officeService.selectOffices(office);
        // 构建列表
        List<SysOffice> officeTreeList = officeService.buildOfficeTree(offices);

        return AjaxResult.success(officeTreeList);
    }

    /**
     * @Description: 根据父id过滤表格数据
     * @Param [parentId]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/filterOfficeTreeList/{parentId}")
    public AjaxResult filterOfficeTreeList(@PathVariable Integer parentId) {
        List<SysOffice> offices = officeService.selectOfficeByParentId(parentId);
        return AjaxResult.success(offices);
    }

    /**
     * @Description: 根据id获取详细信息
     * @Param [id]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/getOfficeById/{id}")
    public AjaxResult getOfficeById(@PathVariable Integer id) {
        SysOffice office = new SysOffice();
        office.setId(id);
        List<SysOffice> offices = officeService.selectOffices(office);
        return AjaxResult.success(offices);
    }

    /**
     * @Description: 获取地区数据
     * @Param []
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/getAreaTree")
    public AjaxResult getAreaTree() {
        List<SysAreaA> list = areaService.list();
        return AjaxResult.success(list);
    }

    /**
     * @Description: 添加机构
     * @Param [office]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @PostMapping("/addOffice")
    @Log(title = "机构管理", businessType = BusinessType.INSERT)
    public AjaxResult addOffice(@RequestBody SysOffice office) {
        if (officeService.addOffice(office)) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * @Description: 修改机构
     * @Param [office]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @PutMapping("/updateOffice")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    public AjaxResult updateOffice(@RequestBody SysOffice office) {
        if (officeService.updateOffice(office)) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * @Description: 删除部门
     * @Param [id]
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @DeleteMapping("/delOffice/{id}")
    @Log(title = "机构管理", businessType = BusinessType.DELETE)
    public AjaxResult deleteOffice(@PathVariable Integer id) {
        if (officeService.deleteOffice(id)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * @Description: 获取所有医院
     * @Param []
     * @Return com.yunyi.common.core.domain.AjaxResult
     * @Throws
     **/
    @GetMapping("/getCompany")
    public AjaxResult getCompany() {
        List<SysOffice> offices = officeService.list(new QueryWrapper<SysOffice>().eq("type", "1").eq("del_flag", "0"));
        return AjaxResult.success(offices);
    }

    @GetMapping("/getOffice/{id}")
    public AjaxResult getOffice(@PathVariable Integer id) {
        return AjaxResult.success(officeService.getOfficeByCompany(id));
    }
}
